package logic

import (
	"context"
	"errors"
	"fmt"
	"gorm.io/gorm"
	"shop_srv/goods"
	"shop_srv/internal/svc"
	"shop_srv/model/mysql"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetGoodsByGoodsIdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetGoodsByGoodsIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetGoodsByGoodsIdLogic {
	return &GetGoodsByGoodsIdLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetGoodsByGoodsIdLogic) GetGoodsByGoodsId(in *goods.GetGoodsByGoodsIdRequest) (*goods.GetGoodsByGoodsIdResponse, error) {
	// todo: add your logic here and delete this line
	g := mysql.Goods{}
	info, err := g.GetInfo(int(in.GoodsId))
	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		fmt.Println("详情查询失败:", err)
		return nil, err
	}
	return &goods.GetGoodsByGoodsIdResponse{
		GoodsTitle:  info.GoodsTitle,
		GoodsPrice:  info.GoodsPrice,
		GoodsStock:  info.GoodsStock,
		GoodsCover:  info.GoodsCover,
		GoodsStatus: int64(info.GoodsStatus),
		GoodsId:     int64(info.ID),
	}, nil
}
